package com.buildings.activity.map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.buildings.R;
import com.buildings.bean.ReinforceResult;
import java.util.List;

/**
 * @author shiyaolei
 * @packager com.buildings.activity.map
 * @date 2020/9/9
 **/
class ReinforceFormListAdapter extends RecyclerView.Adapter<ReinforceFormListAdapter.ViewHolder> {

  private List<ReinforceResult.ReinforceData> data;
  private Activity activity;
  private boolean isEdit;
  private boolean isCommitTips;
  private String status;

  public ReinforceFormListAdapter(Activity activity, List<ReinforceResult.ReinforceData> data) {
    this.data = data;
    this.activity = activity;
  }

  public void update(List<ReinforceResult.ReinforceData> data) {
    this.data = data;
    this.notifyDataSetChanged();
  }

  public void setEdit(boolean isEdit) {
    this.isEdit = isEdit;
  }

  public boolean isEdit() {
    return isEdit;
  }

  public void setCommitTips(boolean isCommitTips) {
    this.isCommitTips = isCommitTips;
  }

  public boolean isCommitTips() {
    return isCommitTips;
  }

  @Override public ReinforceFormListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view =
        LayoutInflater.from(parent.getContext()).inflate(R.layout.item_form_list, parent, false);
    ReinforceFormListAdapter.ViewHolder holder = new ReinforceFormListAdapter.ViewHolder(view);
    view.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View view) {
        if (listener != null) {
          listener.onItemClick(view, (Integer) view.getTag());
        }
      }
    });
    view.setOnLongClickListener(new View.OnLongClickListener() {
      @Override public boolean onLongClick(View v) {
        if (listener != null) {
          listener.onItemLongClick(v, (Integer) v.getTag());
        }
        return false;
      }
    });
    return holder;
  }

  @SuppressLint("SetTextI18n") @Override
  public void onBindViewHolder(final ReinforceFormListAdapter.ViewHolder holder, final int i) {
    final ReinforceResult.ReinforceData bean = data.get(i);
    holder.itemView.setTag(i);
    holder.tvName.setText(bean.name);
    holder.btnSubmit.setVisibility(View.VISIBLE);
    holder.btnEdit.setVisibility(View.VISIBLE);
    holder.btnDelete.setVisibility(View.VISIBLE);
    holder.itemView.setBackgroundColor(activity.getResources().getColor(R.color.white));
    holder.tvName.setTextColor(activity.getResources().getColor(R.color.black));

    String type = bean.status;//表单状态  0未提交  1未审核  2审核未通过 3审核通过
    String completeType = bean.isSaveAll;//未提交的是否已经全部完善 是否填完0、否  1、是

    if (isEdit) {
      holder.checkBox.setVisibility(View.GONE);
      holder.btnSubmit.setVisibility(View.GONE);
      holder.btnEdit.setVisibility(View.GONE);
      holder.btnDelete.setVisibility(View.GONE);

      if (TextUtils.equals(type, "0")) {
        holder.tvStatus.setText("未提交");
        holder.tvStatus.setTextColor(activity.getResources().getColor(R.color.task_status_finished));
      } else if (TextUtils.equals(type, "1")) {
        holder.tvStatus.setText("未审核");
        holder.tvStatus.setTextColor(activity.getResources().getColor(R.color.task_status_finished));
      } else if (TextUtils.equals(type, "2")) {
        holder.tvStatus.setText("审核未通过");
        holder.tvStatus.setTextColor(activity.getResources().getColor(R.color.task_status_unfinished));
      } else if (TextUtils.equals(type, "3")) {
        holder.tvStatus.setText("审核通过");
        holder.tvStatus.setTextColor(activity.getResources().getColor(R.color.task_status_finished));
      }
      if (TextUtils.equals(type, "0")) {//0未提交  1未审核  2审核未通过 3审核通过
        if (TextUtils.equals(completeType, "1") || TextUtils.equals(completeType, "0")) {//未提交的是否已经全部完善 是否填完0、否  1、是
          //未提交且已经全部完善的才显示选择控件
          holder.checkBox.setVisibility(View.VISIBLE);
        }
      }
      if (isCommitTips) {
        if (TextUtils.equals(type, "0")) {
          if (TextUtils.equals(completeType, "0")) {
            holder.tvName.setTextColor(activity.getResources().getColor(R.color.task_status_unfinished));
            holder.tvStatus.setTextColor(activity.getResources().getColor(R.color.task_status_unfinished));
          }
        }
      }
    } else {
      holder.checkBox.setVisibility(View.GONE);
      if (TextUtils.equals(type, "0")) {
        holder.tvStatus.setText("未提交");
        holder.tvStatus.setTextColor(activity.getResources().getColor(R.color.task_status_finished));
        if (TextUtils.equals(completeType, "1")) {
          //显示提交按钮
        } else {
          //未完善，不能提交
          holder.btnSubmit.setVisibility(View.INVISIBLE);
        }
      } else if (TextUtils.equals(type, "1")) {
        holder.tvStatus.setText("未审核");
        holder.tvStatus.setTextColor(activity.getResources().getColor(R.color.task_status_finished));

        holder.btnSubmit.setVisibility(View.INVISIBLE);
        holder.btnEdit.setVisibility(View.INVISIBLE);
        holder.btnDelete.setVisibility(View.INVISIBLE);
      } else if (TextUtils.equals(type, "2")) {
        holder.tvStatus.setText("审核未通过");
        holder.tvStatus.setTextColor(activity.getResources().getColor(R.color.task_status_unfinished));
        holder.btnDelete.setVisibility(View.INVISIBLE);
        holder.btnSubmit.setVisibility(View.INVISIBLE);
      } else if (TextUtils.equals(type, "3")) {
        holder.tvStatus.setText("审核通过");
        holder.tvStatus.setTextColor(activity.getResources().getColor(R.color.task_status_finished));

        holder.btnSubmit.setVisibility(View.INVISIBLE);
        holder.btnEdit.setVisibility(View.INVISIBLE);
        holder.btnDelete.setVisibility(View.INVISIBLE);
      }
    }
    holder.checkBox.setChecked(bean.checked);
    holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        bean.checked=isChecked;
      }
    });

    holder.btnSubmit.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        listener.onSubmit(v, i);
      }
    });
    holder.btnEdit.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        listener.onEdit(v, i);
      }
    });
    holder.btnDelete.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        listener.onDelete(v, i);
      }
    });

    if (TextUtils.equals("2", status)) {
      holder.btnSubmit.setVisibility(View.INVISIBLE);
      holder.btnEdit.setVisibility(View.INVISIBLE);
      holder.btnDelete.setVisibility(View.INVISIBLE);
    }
  }

  @Override public int getItemCount() {
    return data != null ? data.size() : 0;
  }

  public String getStatus() {
    return status;
  }

  public void setStatus(String status) {
    this.status = status;
  }

  public class ViewHolder extends RecyclerView.ViewHolder {
    TextView tvStatus;//状态
    TextView tvName;//名称
    ImageView btnSubmit;//提交
    ImageView btnEdit;//编辑
    ImageView btnDelete;//删除
    CheckBox checkBox;

    public ViewHolder(View itemView) {
      super(itemView);
      tvStatus = itemView.findViewById(R.id.tv_status);
      tvName = itemView.findViewById(R.id.tv_name);
      btnSubmit = itemView.findViewById(R.id.btn_submit);
      btnEdit = itemView.findViewById(R.id.btn_edit);
      btnDelete = itemView.findViewById(R.id.btn_delete);
      checkBox = itemView.findViewById(R.id.checkbox);
    }
  }

  private ReinforceFormListAdapter.OnItemClickListener listener;

  public interface OnItemClickListener {
    void onItemClick(View view, int position);

    void onItemLongClick(View view, int position);

    void onSubmit(View view, int position);

    void onEdit(View view, int position);

    void onDelete(View view, int position);
  }

  public void setOnItemClickListener(ReinforceFormListAdapter.OnItemClickListener listener) {
    this.listener = listener;
  }
}
